//
// Created by yunnan xu on 2020/11/10.
//

#ifndef ANDROIDGRAPHICSDEMOS_SINGLETON_H
#define ANDROIDGRAPHICSDEMOS_SINGLETON_H

#include <thread>
#include <memory>

template<class T>
class Singleton
{
public:

    Singleton (){}
    ~Singleton (){}

    // C++ 11 以上的 局部静态变量 是线程安全的,
    // 参考：https://stackoverflow.com/questions/1661529/is-meyers-implementation-of-the-singleton-pattern-thread-safe

    static T * GetInstance()
    {
        static T t;
        return &t;
    }

    template<class ... Args>
    static T * GetInstance(Args ... args)
    {
        // static std::unique_ptr<T> instance_ptr = std::make_unique<T> (std::forward<Args>(args) ... );
        // return instance_ptr.get();
        static T t (std::forward<Args> (args) ... );
        return &t;
    }
};


#endif //ANDROIDGRAPHICSDEMOS_SINGLETON_H
